home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 3
/
Info_Mac_1994-01.iso
/
Graphics
/
Utility
/
GL Viewer 1.1.1
/
src ƒ
/
memoryio.c
< prev
next >
Wrap
Text File
|
1993-08-28
|
4KB
|
201 lines
/*
* Routines to perform stdio-like in-memory file operations.
*
* Copyright (c) 1993 by Martin W. Fong
*/
#include "memoryio.h"
MemoryFile *
mopen (const char *filename, const char *mode)
{
MemoryFile *retVal = (MemoryFile *) malloc ((size_t) sizeof (MemoryFile));
if (retVal /* != (MemoryFile *) NULL */)
{
retVal->filename = filename;
retVal->buffer = (char *) NULL;
retVal->bufLen = 0L;
retVal->currPos = 0L;
}
return retVal;
}
int
mclose (MemoryFile *stream)
{
if (stream /* != (MemoryFile *) NULL */)
{
if (stream->buffer /* != (char *) NULL */)
free (stream->buffer);
free ((char *) stream);
}
return EOF;
}
int
mseek (MemoryFile *stream, long offset, int whence)
{
if (stream /* != (MemoryFile *) NULL */)
{
long newCurrPos = -1L;
switch (whence)
{
case SEEK_SET:
{
newCurrPos = offset;
break;
}
case SEEK_CUR:
{
newCurrPos = stream->currPos + offset;
break;
}
case SEEK_END:
{
newCurrPos = stream->bufLen - offset;
break;
}
}
if (stream->buffer == (char *) NULL &&
0L < newCurrPos)
{
stream->buffer = (char *) malloc (newCurrPos + 1L);
if (stream->buffer /* != (char *) NULL */)
{
stream->bufLen = newCurrPos + 1;
stream->currPos = 0L;
}
}
if (0L <= newCurrPos && newCurrPos < stream->bufLen)
{
stream->currPos = newCurrPos;
return 0;
}
}
return -1;
}
long
mtell (MemoryFile *stream)
{
if (stream /* != (MemoryFile *) NULL */)
return stream->currPos;
return -1L;
}
size_t
mread (void *array, size_t size, size_t numitems, MemoryFile *stream)
{
if (stream /* != (MemoryFile *) NULL */ &&
stream->currPos < stream->bufLen &&
size /* > 0 */ && numitems /* > 0 */ &&
array /* != (void *) NULL */)
{
unsigned long bytesLeft = stream->bufLen - stream->currPos;
size_t maxNumItems = bytesLeft / size;
unsigned long bytesRead;
if (maxNumItems < numitems)
numitems = maxNumItems;
if ((bytesRead = size * numitems) /* > 0 */)
{
(void) memcpy (array, stream->buffer + stream->currPos, bytesRead);
stream->currPos += bytesRead;
}
return numitems;
}
return (size_t) 0;
}
size_t
mwrite (const void *array, size_t size, size_t numitems, MemoryFile *stream)
{
if (stream /* != (MemoryFile *) NULL */ &&
stream->currPos < stream->bufLen &&
size /* > 0 */ && numitems /* > 0 */ &&
array /* != (void *) NULL */)
{
unsigned long bytesLeft = stream->bufLen - stream->currPos;
size_t maxNumItems = bytesLeft / size;
unsigned long bytesWrote;
if (maxNumItems < numitems)
numitems = maxNumItems;
if ((bytesWrote = size * numitems) /* > 0 */)
{
(void) memcpy (stream->buffer + stream->currPos, array, bytesWrote);
stream->currPos += bytesWrote;
}
return numitems;
}
return (size_t) 0;
}
int
mgetc (MemoryFile *stream)
{
if (stream /* != (MemoryFile *) NULL */ &&
stream->currPos < stream->bufLen)
{
unsigned char uc = (unsigned char ) stream->buffer[stream->currPos++];
return (uc);
}
return EOF;
}
int
mputc (char c, MemoryFile *stream)
{
if (stream /* != (MemoryFile *) NULL */ &&
stream->currPos < stream->bufLen)
{
return (stream->buffer[stream->currPos++] = c);
}
return EOF;
}